package fun.coding.leetcode;

public class LinkedListCycle {

    public static void main(String[] args) {

    }
    
    // It can be mathematically proved this method will eventually meet after K(foot step size)
    public boolean hasCycle(ListNode head) {
        if (head == null) return false;
        
        boolean hasCycle = false;
        
        ListNode r1 = head;
        ListNode r2 = head.next;
        
        while (r2 != null) {
            if (r1 == r2) return true;
            r1 = r1.next;
            // R2 needs to move based on its own location. 
            r2 = r2.next;
            if (r2 == null) return false;
            r2 = r2.next;
        }
        
        return hasCycle;
    }

}
